buildscript {
	ext {
		lombokVersion = '1.18.30'
		springBootVersion = '3.2.0'
	}
}
plugins {
	id 'java'
	id 'pmd'
	id 'jacoco'
	id 'org.springframework.boot' version "${springBootVersion}"
	id 'io.spring.dependency-management' version '1.1.3'
}

group = 'org.jmresler.nibrs'
version = '0.0.1-SNAPSHOT'

java {
	sourceCompatibility = '21'
}

configurations {
	compileOnly {
		extendsFrom annotationProcessor
	}
}

repositories {
	mavenCentral()
}

def integrationTest = sourceSets.create('integration-test')

configurations[integrationTest.implementationConfigurationName].extendsFrom(configurations.testImplementation)
configurations[integrationTest.runtimeOnlyConfigurationName].extendsFrom(configurations.testRuntimeOnly)
configurations[integrationTest.annotationProcessorConfigurationName].extendsFrom(configurations.annotationProcessor)

def integrationTestTask = tasks.register('integrationTest', Test) {
	description = 'Runs integration tests.'
	group = 'verification'
	useJUnitPlatform()

	testClassesDirs = integrationTest.output.classesDirs
	classpath = configurations[integrationTest.runtimeClasspathConfigurationName] + integrationTest.output

	shouldRunAfter(tasks.named('test'))
}

tasks.named('check') {
	dependsOn(integrationTestTask)
}

tasks.named('test') {
	useJUnitPlatform()
	finalizedBy jacocoTestReport // report is always generated after tests run
}

jacocoTestReport {
	dependsOn test // tests are required to run before generating the report
}
jacocoTestCoverageVerification {
	violationRules {
		rule {
			limit {
				minimum = 0.5
			}
		}

		rule {
			enabled = false
			element = 'CLASS'
			includes = ['org.gradle.*']

			limit {
				counter = 'LINE'
				value = 'TOTALCOUNT'
				maximum = 0.3
			}
		}
	}
}

pmd {
	ignoreFailures = true
	consoleOutput = true
	toolVersion = "6.21.0"
	rulesMinimumPriority = 5
	ruleSets =
			["category/java/errorprone.xml",
			 "category/java/codestyle.xml",
			 "category/java/bestpractices.xml",
			 "category/java/design.xml",
			 "category/java/documentation.xml",
			 "category/java/multithreading.xml",
			 "category/java/performance.xml"]
}

clean {
	delete "logs"
	delete "data"

	delete "logs"
	delete "data"
}

tasks.named('test') {
	useJUnitPlatform()
}


dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-batch'
	implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
	implementation 'org.springframework.boot:spring-boot-starter-data-rest'
	implementation 'org.springframework.boot:spring-boot-starter-freemarker'
	implementation 'org.springframework.boot:spring-boot-starter-hateoas'
	implementation 'org.springframework.data:spring-data-rest-hal-explorer'
	compileOnly 'org.projectlombok:lombok'
	developmentOnly 'org.springframework.boot:spring-boot-devtools'
	runtimeOnly 'com.h2database:h2'
	runtimeOnly 'com.mysql:mysql-connector-j'
	implementation 'org.postgresql:postgresql:42.7.1'
	annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
	annotationProcessor 'org.projectlombok:lombok'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
	testImplementation 'org.springframework.batch:spring-batch-test'
}

